import './index.less'
// import '../../lib/layui/layui'
import '../includes/sc-banner/sc-banner.less';
import ImageUpload from '../../components/ImageUpload';
import request from '../../utils/api';
// import 'layuiSrc'

// layui.config({
//     base: '../'
// })
layui.use(['element', 'layer', 'form', 'carousel'], function () {
    let { layer, form, carousel } = layui;
    let formObserver = {};
    carouselInit({ carousel });
    formLayerInit({ layer, form, formObserver });
    formEventInit({ layer, form, formObserver });

    console.log('%cIndex loaded', 'font-size: 16px; color: #16a15f;');
    // console.log('haha')
});

function carouselInit({ carousel }) {
    carousel.render({
        elem: '#news_carousel',
        width: '100%', //设置容器宽度
        height: '100%',
        arrow: 'none', //始终显示箭头
    });

    carousel.render({
        elem: '#info_carousel',
        width: '100%', //设置容器宽度
        height: '100%',
        arrow: 'none', //始终显示箭头
    });
}

function formLayerInit({ layer, form, formObserver } = {}) {
    const hookPioneering = document.querySelector('#pioneering_hook');
    const tempPioneering = tpl_pioneering.innerHTML;
    hookPioneering.onclick = function () {
        formObserver.index = openLayer({
            layer,
            title: '我要创业',
            content: tempPioneering,
            success: function () {
                form.render('select');
                ImageUpload.init({
                    el: '#image_upload2',
                    autoDestroy: true,
                    onChange(...args) {
                        formObserver.onUpload && formObserver.onUpload('idCardPhotoUrl', ...args);
                    }
                });
            }
        });
    }

    const hookInvset = document.querySelector('#invest_hook');
    const tempInvest = tpl_invest.innerHTML;
    hookInvset.onclick = function () {
        formObserver.index = openLayer({
            layer,
            title: '我要投资',
            content: tempInvest,
            success: function () {
                form.render('select');
                ImageUpload.init({
                    el: '#image_upload1',
                    autoDestroy: true,
                    onChange(...args) {
                        formObserver.onUpload && formObserver.onUpload('companyLicensePhoto', ...args);
                    }
                });
            },
        });
    }
}

function openLayer({ layer, title, content, success, done }) {
    return layer.open({
        type: 1,
        skin: 'chj-modal',
        title,
        area: ['700px'],
        btnAlign: 'c',
        // btn: ['提交'],
        content, //这里content是一个普通的String
        success: function () {
            success && success();
        },
        yes: function (...args) {
            done && done(...args);
        }
    });
}

function formEventInit({ form, layer, formObserver }) {
    let uploadData = {};

    form.verify({
        upload: function (value, item) {
            if (!value) {
                return '请上传公司营业执照'
            }
        }
    })

    formObserver.onUpload = function onUpload(key, files, next) {
        // console.log(files);
        request({
            url: '/user/uploadRawPic',
            method: 'post',
            params: {
                file: files[0]
            }
        })
            .then(res => {
                uploadData[key] = res.imgs[0];
                // console.log(res)
                next();
            })
    }

    // 新增创业
    form.on('submit(pionneering)', function (data) {
        console.log(data);
        const { field } = data;
        let body = {
            ...field,
            ...uploadData
        }
        if (!uploadData.idCardPhotoUrl) return layer.msg('请上传身份证照片', { icon: 5, anim: 6 });
        request({
            url: '/shuangchuang/admin/add/innovation',
            method: 'post',
            params: {
                json: JSON.stringify(body)
            }
        })
            .then(res => {
                layer.msg('提交成功', { icon: 6 });
                uploadData = {};
                layer.close(formObserver.index);
            })
        return false;
    })

    // 新增投资
    form.on('submit(invest)', function (data) {
        const { field } = data;

        if (!uploadData.companyLicensePhoto) return layer.msg('请上传公司营业执照', { icon: 5, anim: 6 });
        let body = {
            ...field,
            ...uploadData
        }
        // console.log(body)
        request({
            url: '/shuangchuang/admin/add/investment',
            method: 'post',
            params: {
                json: JSON.stringify(body)
            }
        })
        .then(res => {
            layer.msg('提交成功', { icon: 6 });
            uploadData = {};
            layer.close(formObserver.index);
        })
        return false;
    })
}



